<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

if (!function_exists('site_title')) {
	function site_title($value = '') {
		$CI = &get_instance();
		return $CI->config->item('site_title', 'ion_auth');
	}
}

if (!function_exists('site_tagline')) {
	function site_tagline() {
		$CI = &get_instance();
		return $CI->config->item('site_tagline', 'ion_auth');
	}
}

if (!function_exists('site_desc')) {
	function site_desc() {
		$CI = &get_instance();
		return $CI->config->item('site_desc', 'ion_auth');
	}
}

if (!function_exists('site_email')) {
	function site_email() {
		$CI = &get_instance();
		return $CI->config->item('site_email', 'ion_auth');
	}
}

if (!function_exists('super_admin_group')) {
	function super_admin_group() {
		$CI = &get_instance();
		return $CI->config->item('super_admin_group', 'ion_auth');
	}
}

if (!function_exists('admin_group')) {
	function admin_group() {
		$CI = &get_instance();
		return $CI->config->item('admin_group', 'ion_auth');
	}
}

if (!function_exists('default_group')) {
	function default_group() {
		$CI = &get_instance();
		return $CI->config->item('default_group', 'ion_auth');
	}
}

if (!function_exists('identity')) {
	function identity() {
		$CI = &get_instance();
		return $CI->config->item('identity', 'ion_auth');
	}
}

// User Data

if (!function_exists('user_id')) {
	function user_id() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->id;
		}

		return $data;
	}
}

if (!function_exists('user_first_name')) {
	function user_first_name() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->first_name;
		}

		return $data;
	}
}

if (!function_exists('user_last_name')) {
	function user_last_name() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->last_name;
		}

		return $data;
	}
}

if (!function_exists('user_nickname')) {
	function user_nickname() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->nickname;
		}

		return $data;
	}
}

if (!function_exists('user_display_name')) {
	function user_display_name() {
		$CI = &get_instance();

		if (!$CI->ion_auth->logged_in()){
	    $user ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
		}

    if(!empty($user->nickname)){
      $name = $user->nickname;                                  
    }elseif(!empty($user->first_name) AND !empty($user->last_name)){
    	$name = $user->first_name.' '.$user->last_name;
    }elseif(!empty($user->first_name)){
      $name = $user->first_name;
    }elseif(!empty($user->last_name)){
      $name = $user->last_name;
    }else{
    	$name = '';
    }

    return $name;
	}
}

if (!function_exists('user_username')) {
	function user_username() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->username;
		}

		return $data;
	}
}

if (!function_exists('user_email')) {
	function user_email() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->email;
		}

		return $data;
	}
}

if (!function_exists('user_facebook_id')) {
	function user_facebook_id() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->facebook_id;
		}

		return $data;
	}
}

if (!function_exists('user_twitter_id')) {
	function user_twitter_id() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->twitter_id;
		}

		return $data;
	}
}

if (!function_exists('user_google_id')) {
	function user_google_id() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->google_id;
		}

		return $data;
	}
}

if (!function_exists('user_profile_picture')) {
	function user_profile_picture() {
		$CI = &get_instance();

		if (!$CI->ion_auth->logged_in()){
	    $user ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
		}

		$photo = $CI->session->all_userdata();

    if(!empty($user->profile_picture)){
        $pp = base_url('uploads/' . $user->profile_picture);                                    
    }elseif(!empty($photo['photo_url'])){
        $pp = $photo['photo_url'];
    }else{
        $pp = base_url('themes/general/img/default.jpg');
    }

    return $pp;
	}
}

if (!function_exists('user_profession')) {
	function user_profession() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    $data = $user->profession;
		}

		return $data;
	}
}

if (!function_exists('default_avatar')) {
	function default_avatar() {
    return base_url('themes/general/img/default.jpg');
	}
}

if (!function_exists('user_profile_url')) {
	function user_profile_url() {
		$CI = &get_instance();
		if (!$CI->ion_auth->logged_in()){
	    $data ='';
		}else{
	    $user = $CI->ion_auth->user()->row();
	    if(user_username() !== ''){
	    	$data = site_url(user_username());
	    }else{
	    	$data = site_url(user_id());
	    }
		}

		return $data;
	}
}

if (!function_exists('member_profile_picture')) {
	function member_profile_picture($id) {
		$CI = & get_instance();
    $CI->load->database();

    $CI->db->select('profile_picture');
    $CI->db->where('id', $id);
    $CI->db->or_where('username', $id);
    $query = $CI->db->get('rc_users');

    if ($query->num_rows() > 0) {
        $result = $query->row_array();            
        $query->free_result();
    } else {
        $result = array();
    }

    if(!empty($result['profile_picture'])){
    	$pp = base_url('uploads/' . $result['profile_picture']);
    }else{
    	$pp = default_avatar();
    }

    if(!empty($id)){
    	return default_avatar();
    }else{
    	return $pp;
    }
    
	}
}

function get_profil(){
	$_this = & get_Instance();
	$sql="SELECT * FROM konten_statis WHERE id_konten_statis IN (2,3,4)";
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		$menu= $query->result_array();
	}else{
		$menu= array();
	}
	
	$str_menu="";
	foreach($menu as $val){
		$str_menu.="<li><a href=".site_url('profil/'.$val['slug']).">".$val['nama']."</a></li>";
	}
	return $str_menu;
}

function get_ppk(){
	$_this = & get_Instance();
	$sql="SELECT * FROM konten_statis WHERE id_konten_statis = 5";
	$query=$_this->db->query($sql);
	if ($query->num_rows()>0){
		$menu= $query->result_array();
	}else{
		$menu= array();
	}
	
	$str_menu="";
	foreach($menu as $val){
		$str_menu.="<li><a href=".site_url('ppk').">".$val['nama']."</a></li>";
	}
	return $str_menu;
}